package com.sfh.cpc.user.model;

import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.CacheCity;
import com.sfh.cpc.com.entity.ECity;
import com.sfh.cpc.com.entity.EConsume;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.entity.RspBase;
import com.sfh.cpc.com.entity.RspPage;
import com.sfh.cpc.com.req.ReqConsume;
import com.sfh.cpc.com.req.ReqConsumeWealth;
import com.sfh.cpc.com.req.ReqFile;
import com.sfh.cpc.com.req.ReqQueryUser;
import com.sfh.cpc.com.req.ReqUserActivateVip;
import com.sfh.cpc.com.req.ReqUserLogin;
import com.sfh.cpc.com.req.ReqUserSearch;
import com.sfh.cpc.com.req.ReqUserUpdate;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.event.RxBusEventManager;
import com.sfh.lib.exception.HandleException;
import com.sfh.lib.mvvm.service.BaseViewModel;
import com.sfh.lib.rx.IResult;
import com.sfh.lib.rx.IResultSuccess;
import com.sfh.lib.rx.IResultSuccessNoFail;

import java.util.List;

import io.reactivex.functions.Function;

/**
 * 功能描述: TODO
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/7
 */
public class UserModel extends BaseViewModel {

    public static void login(final IResult<EUser> listener) {
        final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
        if (user != null && user.getUserId() > 0) {
            try {
                listener.onSuccess(user);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            listener.onFail(null);
        }
    }

    public void action(ReqUserActivateVip req) {
        this.execute(false, req, new IResult<RspBase>() {
            @Override
            public void onSuccess(RspBase data) throws Exception {
                if (data.success()) {
                    setValue("onSuccess", data);
                } else {
                    showToast(data.getMsg());
                }
            }

            @Override
            public void onFail(HandleException e) {
                showToast(e.getMsg());
            }
        });
    }

    /***
     * 财富值兑换
     * @param req
     */
    public void consume(ReqConsumeWealth req) {
        this.execute(false, req, new IResult<RspBase>() {
            @Override
            public void onSuccess(RspBase data) throws Exception {
                if (data.success()) {
                    setValue("onSuccess", data);
                } else {
                    showToast(data.getMsg());
                }
            }

            @Override
            public void onFail(HandleException e) {
                showToast(e.getMsg());
            }
        });
    }

    /***
     * 登录
     * @param login
     */
    public void login(ReqUserLogin login) {
        this.execute(false, login, new IResult<RspBase<EUser>>() {
            @Override
            public void onSuccess(RspBase<EUser> data) throws Exception {
                if (data.success()) {
                    AppCacheManager.putCache(BaseAppContent.USER, data.getData(), true);
                    RxBusEventManager.postEvent(data.getData());
                }
                setValue("onLoginSuccess", data);
            }

            @Override
            public void onFail(HandleException e) {
                showDialogToast(e.getMsg());
            }
        });
    }


    /***
     * 查询用户信息
     */
    public void getUser() {
        final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
        if (user != null && user.getUserId() > 0) {
            setValue("onUser", user);
            this.execute(new ReqQueryUser(user.getUserId()), new IResultSuccessNoFail<RspBase<EUser>>() {
                @Override
                public void onSuccess(RspBase<EUser> data) throws Exception {
                    if (data.success()) {
                        AppCacheManager.putCache(BaseAppContent.USER, data.getData(), true);
                        setValue("onUser", data.getData());
                    }
                }
            });
        } else {
            setValue("onLoginToast");
        }
    }


    /***
     * 查询用户信息查看这个帖子与社团相应数据与当前用户财富值
     */
    public void queryConsume(long dataId, int datatype) {
        this.execute(new ReqConsume(dataId, datatype), new IResultSuccess<RspBase<EConsume>>() {
            @Override
            public void onSuccess(RspBase<EConsume> eConsumeRspBase) throws Exception {
                if (eConsumeRspBase.success()) {
                    setValue("onConsumeSuccess", eConsumeRspBase.getData());
                } else {
                    showDialogToast(eConsumeRspBase.getMsg());
                }
            }
        });
    }

    public void upHeadFile(String path) {
        this.execute(new ReqFile(path), new IResult<RspBase<String>>() {
            @Override
            public void onFail(HandleException e) {
                setValue("onHeaderSuccess");
            }

            @Override
            public void onSuccess(RspBase<String> stringRspBase) throws Exception {
                if (stringRspBase.success()) {
                    setValue("onHeaderSuccess", stringRspBase.getData());
                } else {
                    setValue("onHeaderSuccess");
                }
            }
        });
    }

    public void queryCiry() {
        this.execute(new CacheCity().queryCity(), new IResult<List<ECity>>() {
            @Override
            public void onSuccess(List<ECity> data) throws Exception {
                setValue("selectCity", data);
            }

            @Override
            public void onFail(HandleException e) {
                showToast(e.getMsg());
            }
        });
    }

    /***
     * 更新用户信息
     * @param userUpdate
     */
    public void updateUserInfo(final ReqUserUpdate userUpdate) {
        this.execute(true, userUpdate.getTask().map(new Function<RspBase, RspBase>() {
            @Override
            public RspBase apply(RspBase rspBase) throws Exception {
                if (rspBase.success()) {
                    RspBase base = new ReqQueryUser(userUpdate.getUserId()).sendRequest();
                    if (base.success()) {
                        AppCacheManager.putCache(BaseAppContent.USER, base.getData(), true);
                    }
                    return base;
                }
                return rspBase;
            }
        }), new IResultSuccess<RspBase>() {
            @Override
            public void onSuccess(RspBase rspBase) throws Exception {
                if (rspBase.success()) {
                    setValue("onUpdateSuccess");
                } else {
                    showDialogToast(rspBase.getMsg());
                }
            }
        });
    }


    public void querySearchUser(int nowPage, String content) {

        this.execute(new ReqUserSearch(nowPage, content), new IResult<RspBase<RspPage<EUser>>>() {
            @Override
            public void onFail(HandleException e) {
                setValue("onPageSuccess");
            }

            @Override
            public void onSuccess(RspBase<RspPage<EUser>> rspPageRspBase) throws Exception {

                if (rspPageRspBase.success()) {
                    setValue("onPageSuccess", rspPageRspBase.getData());
                } else {
                    setValue("onPageSuccess");
                }
            }
        });
    }
}
